<template>
  <div>
    <!-- 下载暂存区 -->
    <el-tooltip
      :visible="temporaryVisible"
      effect="light"
      placement="bottom-end"
    >
      <el-button
        type="primary"
        link
        v-hasRoles="['service']"
        icon="Download"
        @click.stop.prevent="temporaryShowFun"
      >下载暂存区</el-button>
      <template #content>
        <div class="toolTip_content">
          <div class="card-header">
            <el-icon color="#1890ff">
              <Warning />
            </el-icon>文件生成后将在系统中保留3天，过期将自动删除
          </div>
          <el-divider />
          <el-table
            :data="tableData"
            border
          >

            <el-table-column
              label="申请时间"
              align="center"
              min-width="160"
              prop="createTime"
            />
            <el-table-column
              label="文件地址"
              align="center"
              min-width="160"
              prop="url"
            >
              <template #default="scope">
                <a
                  :href="scope.row.url"
                  target="blank"
                >{{scope.row.url}}</a>
              </template>
            </el-table-column>
            <el-table-column
              label="操作"
              fixed="right"
              align="center"
              width="120"
              class-name="small-padding fixed-width"
            >
              <template #default="scope">
                <el-button
                  link
                  type="primary"
                  @click="downloadLookFun(scope.row)"
                >
                  查看
                </el-button>
                <el-button
                  v-hasRoles="['service']"
                  type="primary"
                  link
                  @click="downloadDelFun(scope.row)"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <pagination
            v-show="total > 0"
            :total="total"
            v-model:page="pages.pageNum"
            v-model:limit="pages.pageSize"
            @pagination="getTableData"
          />
        </div>
      </template>
    </el-tooltip>
  </div>
</template>

<script setup name="DownloadTemporaryStorageArea" lang="ts">
import api from '@/api/workMag/contractInquiry';
import { DownloadListVO } from '@/api/workMag/contractInquiry/types';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const props = defineProps({
  requestMethod: {
    type: String,
    default: ''
  },
  params: {
    type: Object,
    default: () => {}
  }
});

const temporaryShowFun = async () => {
  temporaryVisible.value = !temporaryVisible.value;
  if (temporaryVisible.value) {
    getTableData();
  }
};
const closeTemporary = async () => {
  temporaryVisible.value = false;
};
// 用工签约--start
// 下载暂存区
const temporaryVisible = ref<boolean>(false);
const tableData = ref([]);
const total = ref(0);
const pages = ref<PageQuery>({
  pageNum: 1,
  pageSize: 10
});
const getTableData = async () => {
  let obj = {};
  switch (props.requestMethod) {
    case 'downloadList':
      obj = { ...props.params, ...pages.value };
      break;
    default:
      break;
  }
  const res = await (api as any)[props.requestMethod](obj);
  tableData.value = res.rows;
  total.value = res.total;
};
const downloadLookFun = async (row: DownloadListVO) => {
  // window.open(row.url);
  proxy?.download(`resource/oss/download/${row.ossId}`, {}, `${row.ossId}.zip`);
};
const downloadDelFun = async (row: DownloadListVO) => {
  const res = await api.downloadDel(row.id);
  ElMessage.success('删除成功！');
  getTableData();
};
// 用工签约--end

defineExpose({
  closeTemporary
});
</script>

<style lang="scss" scoped>
.toolTip_content {
  .card-header {
    padding-top: 10px;
    display: flex;
    align-items: center;
    color: #989898;
    font-size: 14px;
    i {
      margin-right: 5px;
    }
  }
  .el-divider {
    margin: 10px 0;
  }
}
</style>
